package com.peng.edu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.edu.entity.Course;
import com.peng.edu.entity.Teacher;
import com.peng.edu.service.CourseService;
import com.peng.edu.service.TeacherService;
import com.peng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/edu/teacherFront")
@CrossOrigin
public class TeacherFrontController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;

    @PostMapping("/getTeacherFrontAll/{page}/{limit}")
    public Result getTeacherFrontAll(@PathVariable Integer page,@PathVariable Integer limit){
        LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Teacher::getId);
        Page<Teacher> pageTeacher = new Page<>(page, limit);
        teacherService.page(pageTeacher, queryWrapper);
        //取出分页的数据放入map集合中
        List<Teacher> records = pageTeacher.getRecords();
        long current = pageTeacher.getCurrent();
        long pages = pageTeacher.getPages();
        long size = pageTeacher.getSize();
        long total = pageTeacher.getTotal();
        //是否有下一页
        boolean hasNext = pageTeacher.hasNext();
        //是否有上一页
        boolean hasPrevious = pageTeacher.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return Result.ok().data(map);
    }

    @GetMapping("/getTeacherAndCourse/{id}")
    public Result getTeacherAndCourse(@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getTeacherId,id);
        List<Course> list = courseService.list(queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("teacher_id",teacher);
        map.put("courseList",list);
        return Result.ok().data(map);
    }
}
